package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PurchaseOrderInfo;
import com.ruoyi.system.mapper.PurchaseOrderMapper;
import com.ruoyi.system.domain.PurchaseOrder;
import com.ruoyi.system.service.IPurchaseOrderService;

/**
 * 采购单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService 
{
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    /**
     * 查询采购单
     * 
     * @param id 采购单主键
     * @return 采购单
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(Long id)
    {
        return purchaseOrderMapper.selectPurchaseOrderById(id);
    }

    /**
     * 查询采购单列表
     * 
     * @param purchaseOrder 采购单
     * @return 采购单
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
    }

    /**
     * 新增采购单
     * 
     * @param purchaseOrder 采购单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPurchaseOrder(PurchaseOrder purchaseOrder)
    {

        purchaseOrder.setStatus("1");
        purchaseOrder.setCreateTime(DateUtils.getNowDate());
        List<PurchaseOrderInfo> purchaseOrderInfoList = purchaseOrder.getPurchaseOrderInfoList();
        BigDecimal totalAmount = BigDecimal.ZERO;
        for (PurchaseOrderInfo purchaseOrderInfo : purchaseOrderInfoList) {
            totalAmount = totalAmount.add(purchaseOrderInfo.getTotalPrice());
        }
        purchaseOrder.setTotalAmount(totalAmount);
        purchaseOrder.setIsInvoiced(0);
        purchaseOrder.setIsReturned(0);
        String userName = SecurityContextHolder.getUserName();
        purchaseOrder.setCreateOrderUser(userName);
        int rows = purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);
        insertPurchaseOrderInfo(purchaseOrder);
        return rows;
    }

    /**
     * 修改采购单
     * 
     * @param purchaseOrder 采购单
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder)
    {
        purchaseOrderMapper.deletePurchaseOrderInfoByPurchaseOrderId(purchaseOrder.getId());
        insertPurchaseOrderInfo(purchaseOrder);
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 批量删除采购单
     * 
     * @param ids 需要删除的采购单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePurchaseOrderByIds(Long[] ids)
    {
        purchaseOrderMapper.deletePurchaseOrderInfoByPurchaseOrderIds(ids);
        return purchaseOrderMapper.deletePurchaseOrderByIds(ids);
    }

    /**
     * 删除采购单信息
     * 
     * @param id 采购单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePurchaseOrderById(Long id)
    {
        purchaseOrderMapper.deletePurchaseOrderInfoByPurchaseOrderId(id);
        return purchaseOrderMapper.deletePurchaseOrderById(id);
    }

    /**
     * 新增采购单明细信息
     * 
     * @param purchaseOrder 采购单对象
     */
    public void insertPurchaseOrderInfo(PurchaseOrder purchaseOrder)
    {
        List<PurchaseOrderInfo> purchaseOrderInfoList = purchaseOrder.getPurchaseOrderInfoList();
        Long id = purchaseOrder.getId();
        if (StringUtils.isNotNull(purchaseOrderInfoList))
        {
            List<PurchaseOrderInfo> list = new ArrayList<PurchaseOrderInfo>();
            for (PurchaseOrderInfo purchaseOrderInfo : purchaseOrderInfoList)
            {
                purchaseOrderInfo.setPurchaseOrderId(id);
                list.add(purchaseOrderInfo);
            }
            if (list.size() > 0)
            {
                purchaseOrderMapper.batchPurchaseOrderInfo(list);
            }
        }
    }
}
